package gait.cannon.web.controller;

import gait.cannon.bean.Article;
import gait.cannon.dao.ArticleDao;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/article")
public class ArticleController {

	public final Log log = LogFactory.getLog(getClass());
	
	private ArticleDao articleDao;
	private String VIEW_TAG = "articleCenter";
	
	@Resource
	public void setArticleDao(ArticleDao articleDao) {
		this.articleDao = articleDao;
	}
	
	@RequestMapping("/list")
	public ModelAndView list() {
		ModelAndView mv = new ModelAndView(VIEW_TAG);
		mv.addObject("articles", articleDao.list());
		return mv;
	}
	
	@RequestMapping("/insert")
	public ModelAndView insert(@RequestParam ("title") String title, @RequestParam ("content") String content) {
		Article article = new Article();
		article.setTitle(title);
		
		String url = null;
		
		article.setUrl(url);
		ModelAndView mv = new ModelAndView(VIEW_TAG);
		articleDao.insert(article);
		mv.addObject("articles", articleDao.list());
		return mv;
	}
	
	@RequestMapping("/delete/{id}")
	public ModelAndView delete(@PathVariable ("id") Integer id) {
		articleDao.delete(id);
		ModelAndView mv = new ModelAndView(VIEW_TAG);
		mv.addObject("articles", articleDao.list());
		return mv;
	}
	
}
